/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "canbusrfidconfig.h"
#include <qsettings.h>
#include <qdebug.h>
#include "domparser.h"

CanbusRfidConfig::CanbusRfidConfig(QString fileName)
{
    isInitialOk=false;
    if(1!=readConfigFile(fileName))
    {
        isInitialOk=false;
    }
    else
    {
        isInitialOk=true;
    }
}


int CanbusRfidConfig::readConfigFile(QString filepath)
{

    DomParser domparser;
    bool ok=domparser.openXml(filepath, QFile::ReadOnly|QFile::Text );
    if(!ok)
    {
        qDebug()<<"error,,,CanbusMaganeticConfig::readConfigFile :"<<ok<< filepath;
        return -1;
    }

    int tmpId;
    domparser.readXml( domparser.findSubNode(domparser.getRootDomElement(), "vendorId", ok), tmpId  );
    if( !ok)
    {

        qDebug()<<"McControllerInterpreter::readRobotInfo====读文件失败: robotType";
        return -1;
    }
    vendorId=(E_VENDOR_ID)tmpId;

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "sdoSetting", ok );
    if( !ok )
    {
            qDebug() << " 读取结点失败：" << filepath;
            return -1;
    }

    QDomNodeList devNodes = domNode.childNodes();
    int ncount=devNodes.size();

    SdoConfig tmpSdoConfig;
    for(int i=0;i<ncount;i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName().indexOf("#comment")>=0){
            continue;
        }

        bool isSucess;
        tmpSdoConfig.index=  devND.toElement().attribute("index" ).toUInt(&isSucess,16);
        tmpSdoConfig.subindex=  devND.toElement().attribute("subindex" ).toUInt(&isSucess,16);
        tmpSdoConfig.byteLength=  devND.toElement().attribute("byteLength" ).toUInt(&isSucess,16);
        tmpSdoConfig.value=  devND.toElement().attribute("value" ).toUInt(&isSucess,16);
        sdoConfigList.append(tmpSdoConfig);

    }

    domparser.closeXml();


    return 1;

}







bool CanbusRfidConfig::isInitialOkStatus()
{
    return isInitialOk;
}

int CanbusRfidConfig::getSdoConfig(QVector<SdoConfig> &sdoConfigListOut)
{
    sdoConfigListOut=sdoConfigList;
    return 1;
}


int CanbusRfidConfig::getVendorId(int& vendorIdReturn)
{
    if(true==isInitialOk)
    {
        vendorIdReturn=vendorId;
        return 1;
    }
    else
    {
        return -1;
    }
}



